package com.germinus.telcoblocks.echarts.wizards;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * 
 * Custom wizard page for getting the necessary information from the user about the new machine
 * 
 *
 * @author Pablo Muñoz
 * 
 */
public class NewEChartsMachineProjectWizardPage extends WizardPage {
	private Combo applicationType;
	private Text packageName;
	private Text className;
	private Text appTitle;
	private ISelection selection;
	
	

	/**
	 * Constructor for NewEChartsMachineProjectWizardPage.
	 * 
	 * @param pageName name for the page
	 */
	public NewEChartsMachineProjectWizardPage(ISelection selection) {
		super("wizardPage");
		setTitle("New ECharts Machine Project");
		setDescription("This wizard creates a new ECharts Machine project.");
		this.selection = selection;
		this.setPageComplete(false);
	}

	/**
	 * Creates the necessary SWT controls 
	 * 
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		layout.verticalSpacing = 9;
		
		Label label1 = new Label(container, SWT.NULL);
		label1.setText("Application type: ");
		applicationType = new Combo(container, SWT.BORDER | SWT.SINGLE);
		applicationType.add("b2bua");
		applicationType.add("proxy");
		GridData gd1 = new GridData(GridData.FILL_HORIZONTAL);
		applicationType.setLayoutData(gd1);
		applicationType.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();		
			}
		});
		
		Label label2 = new Label(container, SWT.NULL);
		label2.setText("Fully-qualified package name: ");
		packageName = new Text(container, SWT.BORDER | SWT.SINGLE);
		packageName.setText("com.example.sipecharts.features.myApp");
		GridData gd2 = new GridData(GridData.FILL_HORIZONTAL);
		packageName.setLayoutData(gd2);
		packageName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label label3 = new Label(container, SWT.NULL);
		label3.setText("Class name for main Machine / proxy application: ");
		className = new Text(container, SWT.BORDER | SWT.SINGLE);
		className.setText("MyApp");
		GridData gd3 = new GridData(GridData.FILL_HORIZONTAL);
		className.setLayoutData(gd3);
		className.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label label4 = new Label(container, SWT.NULL);
		label4.setText("Title (for Javadocs/display-name): ");
		appTitle = new Text(container, SWT.BORDER | SWT.SINGLE);
		appTitle.setText("My Application");
		GridData gd4 = new GridData(GridData.FILL_HORIZONTAL);
		appTitle.setLayoutData(gd4);
		appTitle.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		initialize();		
		dialogChanged();
		setControl(container);
	}

	/**
	 * Tests if the current workbench selection is a suitable container to use.
	 */
	private void initialize() {
		if (selection != null && selection.isEmpty() == false
				&& selection instanceof IStructuredSelection) {
			IStructuredSelection ssel = (IStructuredSelection) selection;
			if (ssel.size() > 1)
				return;
			Object obj = ssel.getFirstElement();
			if (obj instanceof IResource) {
				IContainer container;
				if (obj instanceof IContainer)
					container = (IContainer) obj;
				else
					container = ((IResource) obj).getParent();
				applicationType.setText(container.getFullPath().toString());
			}
		}
	}

	/**
	 * Ensures that both text fields are set.
	 */
	private void dialogChanged() {
		if (getApplicationType().length() == 0) {
			updateStatus("Application type must be set");
			setPageComplete(false);
			return;
		}
		if (getAppTitle().length() == 0) {
			updateStatus("Application title must be specified");
			setPageComplete(false);
			return;
		}
		if (getClassName().length() == 0) {
			updateStatus("Class name must be specified");
			setPageComplete(false);
			return;
		}
		if (getPackageName().length() == 0) {
			updateStatus("Package name must be specified");
			setPageComplete(false);
			return;
		}
		updateStatus(null);
	}

	/**
	 * Updates the status bar
	 * @param message message for the status bar
	 */
	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}

	/**
	 * Getter for the application type parameter
	 * @return application type
	 */
	public String getApplicationType() {
		return applicationType.getText();
	}
	
	/**
	 * Getter for the application title parameter 
	 * @return application title
	 */
	public String getAppTitle() {
		return appTitle.getText();
	}
	
	/**
	 * Getter for the class name parameter
	 * @return class name
	 */
	public String getClassName() {
		return className.getText();
	}
	
	/**
	 * Getter for the package name parameter
	 * @return package name
	 */
	public String getPackageName() {
		return packageName.getText();
	}
}